package baseFx.tests.common.caching;

import baseFx.common.caching.AppCache;
import baseFx.common.caching.Cache;
import baseFx.common.concurrent.Task;
import org.junit.Assert;
import org.junit.Test;

import java.util.UUID;

public class CacheTest {
    public Cache getCache() {
        return new AppCache();
    }

    @Test
    public void 相对时间过期() {
        Cache cache = getCache();
        String key = UUID.randomUUID().toString();
        Assert.assertFalse(cache.exists(key));
        cache.save(i -> {
            i.key = key;
            i.value = 1;
            i.slidingExpiration = 1;
        });
        Assert.assertTrue(cache.exists(key));
        Task.delay(1000);
        Assert.assertFalse(cache.exists(key));
        cache.save(i -> {
            i.key = key;
            i.value = 1;
            i.slidingExpiration = 1;
        });
        Assert.assertTrue(cache.exists(key));
        Task.delay(800);
        cache.get(key);
        Assert.assertTrue(cache.exists(key));
        Task.delay(500);
        Assert.assertTrue(cache.exists(key));
        Task.delay(500);
        Assert.assertFalse(cache.exists(key));
    }
}
